<?php
class Home extends CI_Controller
{
    private static $results_per_page = 4;
    private $terms = array (); // default empty search terms array
    function __construct ()
    {
        // load controller parent
        parent::__construct();
        $this->load->model('Mycommon');
        $this->load->model('ProductsM');
        $this->results_per_page = self::$results_per_page;
//         $this->output->enable_profiler(TRUE);
    }
    function delme ()
    {
        print $this->Mycommon->categoryTreeUL();
    }
    function index ()
    {
        //$this->Mycommon->getCategoryAsUL();       
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = '';
        //        $this->load->model('MPTtree');
        //         $this->MPTtree->set_table('categories');
        //  print ($this->MPTtree->validate());
        $this->load->view('header', $data);
        $this->load->view('home', $data);
        $this->load->view('footer', $data);
    }
    function product ($id = null,$page=null)
    {
         
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'product';
        
        
        
        if ($id!=null){
            //Зареждаме сериите 
            $data['series']=$this->ProductsM->getProductsWhere("category",$id);
            //Зареждаме името на категорията
            $this->db->select ( 'title' );
            $this->db->where ( 'id', $id );
		    $query = $this->db->get ( 'categories' );
		    $data['category_title']=$query->row('title');
		   // $data['category_title2']='Всички серии за '.$query->row('title');
		    
		 //   $a=$this->_getProductsPaging(1,$id);
		    if ($page==null)
		        $page=0;
		    $tmpData=$this->_getProductsPaging($page,$id);
		        
		    $data=array_merge($data,$tmpData);
		    
		     
            $this->load->view('header', $data);
            $this->load->view('product', $data);
            $this->load->view('footer', $data);
        }
        else {
            $this->index();
           
        }
            
        
        
    }
    function promotions ($page=null)
    {
//        $data['tree'] = $this->Mycommon->categoryTreeUL();
//        $data['selected'] = 'promotions';
//        $this->load->view('header', $data);
//        $this->load->view('promotions', $data);
//        $this->load->view('footer', $data);
        
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'promotions';
        
        
        
//        if ($id!=null){
            //Зареждаме promociite
            $data['series']=$this->ProductsM->getProductsWhere("is_promotion",'1');
            
		    $data['category_title']='Промоции';
		    
		 //   $a=$this->_getProductsPaging(1,$id);
		    if ($page==null)
		        $page=0;
		    $data=array_merge($data,$this->_getPromotionsPaging($page));
		    
		     
            $this->load->view('header', $data);
            $this->load->view('promotions', $data);
            $this->load->view('footer', $data);
//        }
//        else {
//            $this->index();
//           
//        }
        
    }
    function projects ($page = null)
    {
        //        $data['tree'] = $this->Mycommon->categoryTreeUL();
//        $data['selected'] = 'promotions';
//        $this->load->view('header', $data);
//        $this->load->view('promotions', $data);
//        $this->load->view('footer', $data);
                $this->load->model('ProjectsM');
        
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'projects';
        
        
        
//        if ($id!=null){
            //Зареждаме projectite
            $data['projectsAll']=$this->ProjectsM->getProjectsWhere();
            
		    $data['category_title']='Проекти';
		    
		 //   $a=$this->_getProductsPaging(1,$id);
		    if ($page==null)
		        $page=0;
		    $data=array_merge($data,$this->_getProjectsPaging($page));
		    
		     
            $this->load->view('header', $data);
            $this->load->view('projects', $data);
            $this->load->view('footer', $data);
    }
    function partners ($id = null)
    {
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'partners';
        $this->load->view('header', $data);
        $this->load->view('partners', $data);
        $this->load->view('footer', $data);
    }
    function about ()
    {
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'about';
        $this->load->model('ContentsM');
        
        $row = $this->ContentsM->getContentsWhere("id", 1);
        $data['aboutus'] = $row[0];
        
        
        $this->load->view('header', $data);
        $this->load->view('about', $data);
        $this->load->view('footer', $data);
    }
    function contacts ($id = null)
    {
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'contacts';
        $this->load->view('header', $data);
        $this->load->view('contacts', $data);
        $this->load->view('footer', $data);
    }

    function productDetails($id=null,$pic_id=null){
        $data['tree'] = $this->Mycommon->categoryTreeUL();
        $data['selected'] = 'product';
    if ($id!=null){
        
            $data['a_pic_id']=$pic_id;
           //zaregdame dannite za producta       
//           $product=$this->ProductsM->getProductsWhere("id",$id);
           $product=$this->ProductsM->getProductsWithCategoryWhere("products.id",$id);
           $data['productDetails']=$product[0];
            //Зареждаме сериите 
            $data['series']=$this->ProductsM->getProductsWhere("category",$data['productDetails']['category']);
            //Зареждаме името на категорията
//            $this->db->select ( 'title' );
//            $this->db->where ( 'id', $id );
//		    $query = $this->db->get ( 'categories' );
//		    $data['category_title']=$query->row('title');
		    $data['category_title']=$product[0]['cat_title'];
		    $images=$this->ProductsM->get_images($id);
		    if (isset($images) && !empty($images))
		      $data['images']=$images;
		    
		    
		     
            $this->load->view('header', $data);
            $this->load->view('product_details', $data);
            $this->load->view('footer', $data);
        }
        else {
            $this->index();
           
        }
//        $this->load->view('header', $data);
//        $this->load->view('product_details', $data);
//        $this->load->view('footer', $data);
    }
    function projectDetails($id=null,$pic_id=null){
            $data['tree'] = $this->Mycommon->categoryTreeUL();
            $data['selected'] = 'projects';
        if ($id!=null){
                 $this->load->model('ProjectsM');
                $data['a_pic_id']=$pic_id;
               //zaregdame dannite za producta       
               $product=$this->ProjectsM->getProjectsWhere("id",$id);
               $data['projectDetails']=$product[0];
    
               //Зареждаме vsichki projecti 
                $data['projectsAll']=$this->ProjectsM->getProjectsWhere();
    		    $data['project_title']=$product[0]['title'];
    		    $images=$this->ProjectsM->get_images($id);
    		    if (isset($images) && !empty($images))
    		      $data['images']=$images;
    		    
    		    
    		     
                $this->load->view('header', $data);
                $this->load->view('project_details', $data);
                $this->load->view('footer', $data);
        }
        else {
            $this->projects();
           
        }
    } 
    function _getProductsPaging($page=null,$catId=null) {
      
        $this->load->library ( 'pagination' );
//        if (is_null($page))
//			$offset = $this->uri->segment ( 4, 0 );
//		else
		    $offset = $page;
//var_dump($this->session->all_userdata());
		$this->session->unset_userdata('s_category');
		$this->session->unset_userdata('s_title');
		$this->session->unset_userdata('s_promotion');
		$this->terms ['s_category'] = $catId;//$this->input->post ( 's_category' );   
		$this->session->set_userdata ( $this->terms );
		
		$total = $this->ProductsM->count_all (); 
		$data ['products'] = $this->ProductsM->get_all ( $this->results_per_page, $offset );
		$data ['total_rows'] = $total;
		
		$config ['base_url'] = site_url () . '/home/product/'.$catId;
		$config ['total_rows'] = $total;
		$config ['per_page'] = $this->results_per_page;
		$config ['uri_segment'] = 4;
		$config ['num_links'] = 3;
		$config['next_link'] = 'Следваща &gt;';
		$config['prev_link'] = '&lt; Предишна';
		
		$this->pagination->initialize ( $config );
		
		$data ['page_links'] = $this->pagination->create_links ();
		$data ['offset'] = $offset;
		$data ['currentPage']= floor(($offset/$config['per_page']) + 1); 
		return $data;     
    }
function _getPromotionsPaging($page=null) {
        $this->load->library ( 'pagination' );
//        if (is_null($page))
//			$offset = $this->uri->segment ( 4, 0 );
//		else
		    $offset = $page;

		    
		$this->session->unset_userdata('s_category');
		$this->session->unset_userdata('s_title');
		$this->session->unset_userdata('s_promotion');
		$chk = $this->session->userdata ( 's_promotion');
		
		$this->terms ['s_promotion'] = 1;//$this->input->post ( 's_category' );   
		$this->session->set_userdata ( $this->terms );
		
		$total = $this->ProductsM->count_all (); 
		$data ['products'] = $this->ProductsM->get_all ( $this->results_per_page, $offset );
		$data ['total_rows'] = $total;
		
		$config ['base_url'] = site_url () . '/home/promotions/';
		$config ['total_rows'] = $total;
		$config ['per_page'] = $this->results_per_page;
		$config ['uri_segment'] = 3;
		$config ['num_links'] = 3;
		$config['next_link'] = 'Следваща &gt;';
		$config['prev_link'] = '&lt; Предишна';
		
		$this->pagination->initialize ( $config );
		
		$data ['page_links'] = $this->pagination->create_links ();
		$data ['offset'] = $offset;
		$data ['currentPage']= floor(($offset/$config['per_page']) + 1); 
		return $data;     
    }
    
function _getProjectsPaging($page=null) {
        $this->load->library ( 'pagination' );
//        if (is_null($page))
//			$offset = $this->uri->segment ( 4, 0 );
//		else
		    $offset = $page;

		    
		$this->session->unset_userdata('s_category');
		$this->session->unset_userdata('s_title');
		$this->session->unset_userdata('s_promotion');
		   
		//$this->session->set_userdata ( $this->terms );
		
		$total = $this->ProjectsM->count_all (); 
		$data ['projects'] = $this->ProjectsM->get_all ( $this->results_per_page, $offset );
		$data ['total_rows'] = $total;
		
		$config ['base_url'] = site_url () . '/home/projects/';
		$config ['total_rows'] = $total;
		$config ['per_page'] = $this->results_per_page;
		$config ['uri_segment'] = 3;
		$config ['num_links'] = 3;
		$config['next_link'] = 'Следваща &gt;';
		$config['prev_link'] = '&lt; Предишна';
		
		$this->pagination->initialize ( $config );
		
		$data ['page_links'] = $this->pagination->create_links ();
		$data ['offset'] = $offset;
		$data ['currentPage']= floor(($offset/$config['per_page']) + 1); 
		return $data;     
    }
}
?>